/*******************************************************
> Copyright (C) 2024 ==IOT== All rights reserved.
> File Name: main.cpp
> Author: IOT
> Website:www.slq.org
> Created Time: 2024年10月28日 星期一 18时27分54秒
***************************************************/

#include <iostream>
#include "fun_template.h"
using namespace std;

// 定义drinks类
class Drinks { // 本质还是类型
private: // 通过私有属性保护成员变量
	string brand; // 品牌
	float price;  // 价格
	int amount;	  // 数量
	
public: // 公有
	// 特殊接口
	Drinks() // 成员构造函数(默认构造)
	{
		//cout << "consturctor" << endl;
		brand = "";
		price = 0.0;
		amount = 0;
	}
	
	Drinks(const string &_brand, float _price, int _amount)
	{
		brand = _brand;
		price= _price;
		amount = _amount;
	}
	
	~Drinks()
	{
		cout << "deconstructor" << endl;
	}
	void set_brand(const string &_brand)
	{
		brand = _brand;
	}
	string get_brand()
	{
		return brand;
	}
	void set_price(float _price) // 成员函数
	{
		if (_price > 0)
			price = _price;	 // 类内可以访问私有的成员变量
	}
	float get_price(); // 成员函数可以在类内声明
	int get_amount()
	{
		return amount;
	}
	void show()
	{
		cout << "品牌:" << brand << ", 价格:" << price << "，数量" <<amount << endl;
	}
	Drinks operator=(Drinks &drk)
	{
		brand = drk.brand;
		price = drk.price;
		amount = drk.amount;
		
		return *this;
	}
};
/*
函数功能：
*/
float Drinks::get_price() // 类外定义，::域作用运算符，说明该函数属于哪一个类
{
	return price;
}



void print_drinks(Drinks *pdrk, int len)
{
	for (int i = 0;i < len; i++) {
		pdrk[i].show();
	}
}

// 普通函数
bool drink_cmp(Drinks &d1, Drinks &d2)
{
	return d1.get_price() > d2.get_price();
}
// 仿函数
struct CmpAmount {
	bool operator() (Drinks &d1, Drinks &d2)
	{
		return d1.get_amount() > d2.get_amount();
	}
};

int main(int argc,const char* argv[])
{
	Drinks slot[3] = {Drinks("可乐", 5.0, 12), Drinks("雪碧", 4.0, 22), Drinks("红牛", 8.0, 37)};
    
	print_drinks(slot, 3);
	mySort(slot, 3, drink_cmp);
	print_drinks(slot, 3);
	
	CmpAmount cmp_amount;
	mySort(slot, 3, cmp_amount);
	print_drinks(slot, 3);
	
	return 0;
}
